home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / FileLib / FileArchiveLib.c next >
Encoding:
C/C++ Source or Header  |  1993-03-22  |  2.4 KB  |  98 lines  |  [TEXT/KAHL]

  1. /* Source code for Authenticator by Eclectic Associates
  2.     
  3.     Functions for remembering (ie, archiving) a file's location to disk.
  4.     Useful for remembering the location of a file between runs of an
  5.     application.
  6.     
  7.     Revision History:
  8.     
  9.     91/01/24 AIH
  10.     - Created this file by extracting the archiving functions from the
  11.     file library.
  12. */
  13.  
  14. #include "ResourceTypeLib.h"
  15. #include "ArchiveLib.h"
  16. #include "FileArchiveLib.h"
  17.  
  18. /* structure used to remember a file's location */
  19. typedef struct {
  20.         FileDirType    dir;    /* directory ID */
  21.         FileNmType    name;    /* name of file */
  22.         CStr31        volnm;/* name of volume */
  23. } FileArchiveType;
  24.  
  25. /* Archive the file to the currently open resource fork. The file
  26.     is archived as a resource of type RSRC_FILE_TYPE and with the given
  27.     ID. This function is most useful for remembering the location of
  28.     a file after quiting an application. */
  29. OSErr FileArchiveWrite(FileType *fp, short id)
  30. {
  31. BEGIN
  32.     FileArchiveType    ar;
  33.     HVolumeParam        pb;
  34.     
  35.     require(FileValid(fp));
  36.  
  37.     /* clear archive */
  38.     memset(&ar, sizeof(FileArchiveType), 0);
  39.  
  40.     /* get name of volume */
  41.     memset(&pb, sizeof(ParamBlockRec), 0);
  42.     pb.ioNamePtr = (StringPtr) ar.volnm;
  43.     pb.ioVRefNum = fp->vol;
  44.     pb.ioVolIndex = 0;
  45.     fp->error = PBHGetVInfo(&pb, false);
  46.     if (! fp->error) {
  47.     
  48.         /* fill in fields of archive */
  49.         strcpy(ar.name, fp->cnm);
  50.         PtoCstr(ar.volnm);
  51.         ar.dir = fp->dir;
  52.         
  53.         /* write archive */
  54.         fp->error = ArchiveWriteRsrc(RSRC_FILE_TYPE, id, &ar, sizeof(ar), FILE_VERSION);
  55.     }
  56.     return(fp->error);
  57. END
  58. }
  59.  
  60. /* This is the inverse of FileArchiveWrite. This function reads
  61.     in a resource of type FILE_ARCHIVE with the given ID, and
  62.     sets up the file reference so that you can call FileOpen
  63.     to open the archived file. The volume containing the file
  64.     must be mounted when this function is called. */
  65. OSErr FileArchiveRead(FileType *fp, short id)
  66. {
  67. BEGIN
  68.     FileArchiveType    ar;
  69.     HVolumeParam        pb;
  70.     size_t                size;
  71.     long                    version;
  72.  
  73.     /* read archive */
  74.     size = sizeof(FileArchiveType);
  75.     version = FILE_VERSION;
  76.     fp->error = ArchiveReadRsrc(RSRC_FILE_TYPE, id, &ar, &size, &version);
  77.     if (! fp->error) {
  78.         
  79.         /* convert volume name to volume reference number */
  80.         memset(&pb, sizeof(ParamBlockRec), 0);
  81.         CtoPstr(ar.volnm);
  82.         pb.ioNamePtr = (StringPtr) ar.volnm;
  83.         pb.ioVRefNum = 0;
  84.         pb.ioVolIndex = -1;
  85.         fp->error = PBHGetVInfo(&pb, false);
  86.         
  87.         /* set file */
  88.         if (! fp->error)
  89.             FileSet(fp, pb.ioVRefNum, ar.dir, ar.name);
  90.     }
  91.     
  92.     ensure(fp->error || FileValid(fp));
  93.     
  94.     return(fp->error);
  95. END
  96. }
  97.  
  98.